//
//  KKJiFenController.m
//  DoMi
//
//  Created by 康凯 on 2016/11/10.
//  Copyright © 2016年 kk. All rights reserved.
//

#import "KKJiFenController.h"
#import "KKBillCell.h"
#import "KKMyHttp.h"
#import "KKWebViewVC.h"

@interface KKJiFenController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UILabel *currentJiFen;

@property (weak, nonatomic) IBOutlet UIView *jiFenActivityView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (assign, nonatomic) int page;
@property (strong, nonatomic)NSMutableArray *datas;

@end

@implementation KKJiFenController
-(NSMutableArray *)datas{
    if(!_datas){
        _datas=[NSMutableArray array];
    }
    return _datas;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"我的积分";
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(jiFenActivity)];
    [self.jiFenActivityView addGestureRecognizer:tap];
    
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    self.tableView.rowHeight=41;
    [self.tableView registerNib:[UINib nibWithNibName:@"KKBillCell" bundle:nil] forCellReuseIdentifier:@"KKBillCell"];
    [self getCurrentPrice];
    self.page=1;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getAccountDetail)];
    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page++;
        [self loadMoreData];
    }];
}

#pragma mark 积分活动
-(void)jiFenActivity{
    KKWebViewVC *webView=[[KKWebViewVC alloc] init];
    webView.navTitle=@"积分优惠活动";
    webView.url=[NSString stringWithFormat:@"%@%@",SERVER_HOST,@"Index/jfyh"];
    [self.navigationController pushViewController:webView animated:YES];
}

#pragma mark tableview代理
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    KKBillCell *billCell=(KKBillCell *)cell;
    [billCell initJiFenData:self.datas[indexPath.row]];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datas.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [tableView dequeueReusableCellWithIdentifier:@"KKBillCell"];
}


#pragma mark 接口--当前余额
-(void)getCurrentPrice{
    [KKMyHttp getCurrentJiFenSuccess:^(id data) {
        self.currentJiFen.text=data;
    } failure:^(NSError *error) {
        
    }];
    
}
#pragma mark 接口--帐户明细
-(void)getAccountDetail{
    [KKMyHttp getJiFenDetail:@"1" success:^(id data) {
        self.page=1;
        [self.datas removeAllObjects];
        [self.datas addObjectsFromArray:data];
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
    }];
}
#pragma mark 接口--帐户明细
-(void)loadMoreData{
    [KKMyHttp getJiFenDetail:[NSString stringWithFormat:@"%d",self.page] success:^(id data) {
        [self.datas addObjectsFromArray:data];
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
    } failure:^(NSError *error) {
        [self.tableView.mj_footer endRefreshing];
    }];
}

@end
